//
//  MZRecommendLastTableViewCell.m
//  OurTourism
//
//  Created by 孟镇 on 15/12/2.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "MZRecommendLastTableViewCell.h"
#import "Header.h"

@interface MZRecommendLastTableViewCell () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UIImageView *photo;
@property (nonatomic, strong)UILabel *userNameLabel;
@property (nonatomic, strong)UILabel *titleLabel;//文章标题
@property (nonatomic, strong)UILabel *subTitleLabel; //副标题
@property (nonatomic, strong)UICollectionView *collectionView; //装图片
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;

@end

@implementation MZRecommendLastTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.photo = [[UIImageView alloc]init];
        [self.contentView addSubview:self.photo];
        
        self.userNameLabel = [[UILabel alloc ]init];
        self.userNameLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.userNameLabel];
        
        self.titleLabel = [[UILabel alloc]init];
        [self.contentView addSubview:self.titleLabel];
        self.titleLabel.textColor = [UIColor colorWithRed:0.36 green:0.75 blue:0.8 alpha:1];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        
        self.subTitleLabel = [[UILabel alloc]init];
        [self.contentView addSubview:self.subTitleLabel];
        self.subTitleLabel.numberOfLines = 5;
        self.subTitleLabel.font = [UIFont systemFontOfSize:14];
        
        self.layout = [[UICollectionViewFlowLayout alloc]init];
        self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.layout.minimumInteritemSpacing = 10;
        self.layout.minimumLineSpacing = 15;
        
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout] ;
        self.collectionView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
        [self.contentView addSubview:self.collectionView];
        self.collectionView.showsHorizontalScrollIndicator = NO;
        
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"itemLocation"];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.photo.frame = CGRectMake(10, 13, WIDTH / 10, WIDTH / 10);
    self.photo.layer.masksToBounds = YES;
    self.photo.layer.cornerRadius = WIDTH / 20;
    self.userNameLabel.frame = CGRectMake(10 + self.photo.frame.size.width + 20, self.photo.frame.origin.y, WIDTH - 10 - self.photo.frame.size.width - 20 - 10 , 10);
    self.titleLabel.frame = CGRectMake(self.userNameLabel.frame.origin.x, self.userNameLabel.frame.origin.y + 10 + 5,self.userNameLabel.frame.size.width , 10);
    
    
    self.layout.itemSize = CGSizeMake( 200, 150);
    
    
    CGFloat hSubtitle = [[self class]heightWithText:self.tempDic[@"trip_text"] fontSize:14 Width:WIDTH - 20];
    self.subTitleLabel.frame = CGRectMake(self.photo.frame.origin.x,self.photo.frame.origin.y +self.photo.frame.size.height + 15 , WIDTH - 20, hSubtitle);
    
    
    if ([self.tempDic[@"trip_text"] length] != 0) {
        self.collectionView.frame  = CGRectMake(10, self.subTitleLabel.frame.origin.y + self.subTitleLabel.frame.size.height + 10, WIDTH - 20, 150);
        
    }
    else
    {
        self.collectionView.frame  = CGRectMake(10, self.photo.frame.origin.y + self.photo.frame.size.height + 15, WIDTH - 20, 150);
        
    }
}

- (void)setTempDic:(NSDictionary *)tempDic
{
    if (_tempDic != tempDic) {
        _tempDic = tempDic;
    }
    
    self.titleLabel.text = [NSString stringWithFormat:@"%s %@","@", tempDic[@"trip"][@"name"]];
    self.subTitleLabel.text = tempDic[@"trip_text"];
    [self.photo sd_setImageWithURL:[NSURL URLWithString:tempDic[@"trip"][@"user"][@"avatar_l"]] placeholderImage:nil];
    self.userNameLabel.text = tempDic[@"trip"][@"user"][@"name"];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.tempDic[@"waypoints"] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemLocation" forIndexPath:indexPath];
    UIImageView *backgroundImg = [[UIImageView alloc] initWithFrame:cell.contentView.frame];
    [backgroundImg sd_setImageWithURL:[NSURL URLWithString:self.tempDic[@"waypoints"][indexPath.item][@"photo"]] placeholderImage:[UIImage imageNamed:@"collectionItem"]];
    backgroundImg.contentMode = UIViewContentModeScaleAspectFill;
    backgroundImg.clipsToBounds = YES;
    cell.backgroundView = backgroundImg;
    
    return cell;
}


//计算文本高度的方法
+ (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
